import Vue from 'vue'
import VueRouter from 'vue-router'

import routes from './routes'
import store from '../store/index'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

router.beforeEach((to, from, next) => {
    if (to.meta && to.meta.title) {
        store.state.title = to.meta.title
    }
    
    if(sessionStorage.getItem("isReload")) {
        let r = store.state.historyRoutes.find(data => data.path === to.fullPath)
        to.fullPath != '/index' && localStorage.setItem('backUrl',from.fullPath)
        if(!r && to.fullPath != '/index'){
            store.commit('addHistoryRoute', to)
        }
    }

    next()
})

export default router
